package com.xr.wage;

import com.xr.common.BusinessTypeVar;
import com.xr.common.CommUtil;
import com.xr.common.Tools;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;

/**
 * dsListener
 */
public class dsListener extends AbstractDatasetListener {
  public boolean beforeLoadData(Dataset dataset)
     throws Exception {
	  String dsName = dataset.parameters().getString("dsName");
		String sql = "";
			SqlDataset ds = (SqlDataset) dataset;
			DoradoContext context = DoradoContext.getContext();
			 
			if(dsName != null && dsName.equals("sendList_datasetSend")) {
				sql=ds.getSql();
				
				if(ds.parameters().getLong("MANAGE_ID")>0){
					if(ds.parameters().getString("MANAGE_ID").equals(CommUtil.getEmployeeId())){
						 sql+=" and ws.owner_id="+ds.parameters().getString("MANAGE_ID");
					}else{
						 sql+=" and ws.if_checkout=1 and ws.owner_id="+ds.parameters().getString("MANAGE_ID");	
					}
					
				}else{
					sql+=" and exists(select 1 from rt_user_deal_emp ude where ude.employee_id=ws.owner_id and ude.man_employee_id="+CommUtil.getEmployeeId()+") ";
					//sql+=" and ws.owner_id="+CommUtil.getEmployeeId()+" ";
				}

				sql += CommUtil.setConditionSQL(ds)+" order by ws.SEND_MONTH desc,ws.SEND_ID desc "; 
				ds.setSql(sql);
			}
			
			if(dsName != null && dsName.equals("dataImport_datasetSend")) {
				sql=ds.getSql();
				
				sql+=" and ws.owner_id="+CommUtil.getEmployeeId()+" ";

				sql += CommUtil.setConditionSQL(ds)+" order by ws.SEND_MONTH desc,ws.SEND_ID desc "; 
				ds.setSql(sql);
			}
			
			
			
			if(dsName != null && dsName.equals("sendListAll_datasetSend")) {
				sql=ds.getSql();
				sql += CommUtil.setConditionSQL(ds)+" order by ws.SEND_MONTH desc,ws.SEND_ID desc "; 
				ds.setSql(sql);
				 
			}
			if(dsName != null && dsName.equals("checkSendList_datasetSend")) {
				sql=ds.getSql();
				sql+=CommUtil.setConditionSQL(ds);
				sql+=" and exists(select 1 from wa_psn psn where psn.send_id=ws.send_id ";
				sql += CommUtil.getDataRightSql("psn", BusinessTypeVar.wage_checkSendList_datasetSend);
				sql+=")";
				sql += " order by ws.SEND_MONTH desc,ws.SEND_ID desc "; 
				ds.setSql(sql);
				 
			}	
			if(dsName != null && dsName.equals("memoRemindSelectEmp_datasetQueryEmployee")) {
				String empIds=ds.parameters().getString("empids");
				String dsNameSub =ds.parameters().getString("dsNameSub");
				if(dsNameSub.equals("checkClass")){
					sql="select emp.employee_id,com.company_name,d.dept_name,emp.employee_name," +
							"emp.employee_code,oos.onoff_station_id,d.dept_id,oos.company_id,p.position_name,p.position_id " +
							"from org_employee emp,org_onoff_station oos,org_company com,org_dept d,pb_position p," +
							"(select distinct ude.man_employee_id from rt_user_deal_emp ude where ude.business_type=300) ude " +
							"where oos.company_id=com.company_id and oos.dept_id=d.dept_id and " +
							"emp.employee_id=oos.employee_id and oos.position_id=p.position_id and ude.man_employee_id=emp.employee_id ";
					sql += CommUtil.setConditionSQL(ds)+" "; 
					ds.setSql(sql);
				}else{
				sql=ds.getSql();
				sql+=" and emp.employee_id not in("+empIds+") ";
				//sql += CommUtil.getDataRightSql("oos", BusinessTypeVar.wage_classItem_dsListener_empClassBankPayer_datasetCBP);
				sql += CommUtil.setConditionSQL(ds)+" "; 
				ds.setSql(sql);
				}
			}
			
			if(dsName != null && dsName.equals("memoRemindSelectEmp_datasetRemindEmp")) {
				String empIds=ds.parameters().getString("empids");
				sql=ds.getSql();
				sql+=" and emp.employee_id in("+empIds+") ";
				sql += CommUtil.setConditionSQL(ds)+" "; 
				ds.setSql(sql); 
			}
			if(dsName != null && dsName.equals("waClass_datasetWaClass")) {
				sql=ds.getSql(); 
				sql += CommUtil.setConditionSQL(ds)+" "; 
				ds.setSql(sql+" order by CREATE_DATE"); 
			}
			if(dsName != null && dsName.equals("sendNew_datasetPlan")) {
				sql=ds.getSql(); 
				sql += CommUtil.setConditionSQL(ds)+" "; 
				ds.setSql(sql+" order by ocp.run_order,wage_date"); 
			}
			if(dsName != null && dsName.equals("waTimetable_datasetPlan")) {
				String sendMonth=Tools.format(ds.parameters().getDate("SEND_MONTH"),"yyyyMM");
				sql="select ocp.*,add_months(to_date('"+sendMonth+"01','yyyyMMdd'),ocp.LABOR_MONTH)LABOR_MONTH1,add_months(to_date('"+sendMonth+"01','yyyyMMdd'),ocp.TAX_MONTH)TAX_MONTH1, " +
						"(select waclass_name from wa_class where waclass_id=ocp.waclass_id)waclass_name," +
						"(select employee_name from org_employee where employee_id=ocp.MANAGER_ID) MANAGER," +
						"(add_months(to_date('"+Tools.format(ds.parameters().getDate("SEND_MONTH"),"yyyyMM")+"'||to_char(Floor(ocp.wage_date),'00') ,'yyyy-MM-dd'),ocp.SEND_MONTH_DATE)-ocp.ahead_days)will_run_date," +
						"(select count(1) from wa_send ws,wa_psn wp where ws.send_id=wp.send_id and ws.send_month='"+sendMonth+"' and ws.ooc_class_plan_id=ocp.ooc_class_plan_id)sendc, " +
						"((select count(1) from wa_send ws,wa_psn wp where ws.send_id=wp.send_id and ws.send_month=to_char(add_months(to_date('"+sendMonth+"01','yyyyMMdd'),-1),'yyyyMM') and ws.waclass_id=ocp.waclass_id))lastPsnC"+
						" from WA_OOC_CLASS_PLAN ocp where 1=1 "; 
				sql += CommUtil.setConditionSQL(ds)+" "; 
				ds.setSql(sql+" order by ocp.run_order"); 
			}
			if(dsName != null && dsName.equals("waTimetable_datasetSend")) {
				sql=ds.getSql(); 
				sql += CommUtil.setConditionSQL(ds)+" "; 
				ds.setSql(sql); 
			}
			return true;
  }

}
